import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getTenantID, getUserID, handleCheckSearchParams, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { HomeTaskTabSwitch, getHomeTabSwitch, homeTaskSearchConfig } from '../mconfig';

const baseUrl = getBaseUrl();

/**
 * 管理页面处理数据
 * @param curRes  权限参数
 */
const handlCheckManageService = (curRes: Record<string, any>, callback: (res: any) => void) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.PURAPPLY_STATE}`];

  /** 获取权限参数 */
  const queryParams: Record<string, any> = HomeTaskTabSwitch(curRes);

  /** 获取客户列表数据 */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const urlTask: string = opt?.taskType ? '/purchaseApplyController/hisTaskDataGrid' : '/purchaseApplyController/taskDataGrid/0';

      let requestURL = `${baseUrl}${urlTask}/${queryParams?.queryType}`;
      let requestBody: Record<string, any> = {
        current: 1,
        pageSize: 10,
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
        ...queryParams,
        dealState: opt?.taskType, // taskType  代办flase 已办true
        sort: opt?.taskType ? 'END_TIME_' : 'CREATE_TIME_',
      };

      delete requestBody['taskType'];

      const defaultSearchClounm = homeTaskSearchConfig();

      const requestParams = handleCheckSearchParams(defaultSearchClounm, requestBody, 'purchase_apply');

      return requestFunComponFiex('POST', requestURL, requestParams);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
      onSuccess: callback,
    },
  );

  /* 获取页面数据 */
  const getTaskCheckDataApply = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    if (request?.applyTime) {
      const [applyTimeStart, applyTimeEnd] = request.applyTime;
      request['applyTimeStart'] = applyTimeStart.format('YYYY-MM-DD 00:00:00');
      request['applyTimeEnd'] = applyTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['applyTime'];
    }

    if (request?.createrID && request?.createrID?.length > 0) {
      request['createrID'] = request?.createrID?.toString();
    } else {
      delete request['createrID'];
    }

    run({
      ...request,
    });
  };

  /** 页面列表 */
  let returnTableProps = useMemo(() => {
    return {
      dataApply: data,
      paginationApply: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    taskLoadingApply: loading,
    getTaskCheckDataApply,
    reloadTaskDataApply: refresh,
    ...returnTableProps,
  };
};

export { handlCheckManageService };
